package com.youme.section.shop.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.hjq.toast.ToastUtils
import com.hyphenate.easeim.R
import com.salton123.app.BaseApplication
import com.youme.model.Food
import com.youme.model.FoodAdd
import com.youme.section.shop.AddCaiListenerr
import kotlinx.android.synthetic.main.item_shop_details_menu_right.view.*

class MenuRightAdapter(private val data: MutableList<Food>) : RecyclerView.Adapter<MenuRightAdapter.MyViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)

    private var destid = ""

    private lateinit var addCaiListenerr: AddCaiListenerr
    private lateinit var addCaiListenerr2: AddCaiListenerr
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_shop_details_menu_right, parent, false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    public fun setdestId(id: String) {
        destid = id
    }

    public fun setAddListener(listenerr: AddCaiListenerr) {
        addCaiListenerr = listenerr
    }

    public fun setAddListener2(listenerr: AddCaiListenerr) {
        addCaiListenerr2 = listenerr
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val textpos = data[position]
        holder.itemView.view_bottom.visibility = if (position == data.size - 1) {
            View.VISIBLE
        } else {
            View.GONE
        }
        holder.itemView.iv_add_shopping_cart.setOnClickListener {
            if ("null".equals(destid) || destid.isNullOrBlank()) {
                ToastUtils.show("请先选择桌号")
                return@setOnClickListener
            }
            var foodAdd = FoodAdd("", destid, textpos.productId, textpos.name, textpos.price, 1)
            addCaiListenerr.addCai(foodAdd)
            addCaiListenerr2.addCai(foodAdd)

        }
        holder.itemView.tv_sku.setText("销量 ： " + textpos.saleNum)
        holder.itemView.tv_goods_name.setText(textpos.name)
        holder.itemView.tv_goods_price.setText("" + textpos.price)
        Glide.with(BaseApplication.sInstance)
            .load(textpos.icon).into(holder.iv_goods)
    }

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val iv_goods: ImageView = itemView.findViewById(R.id.iv_goods)
        val tv_goods_name: TextView = itemView.findViewById(R.id.tv_goods_name)
        val tv_sku: TextView = itemView.findViewById(R.id.tv_sku)
        val tv_goods_price_unit: TextView = itemView.findViewById(R.id.tv_goods_price_unit)
        val tv_goods_price: TextView = itemView.findViewById(R.id.tv_goods_price)
        val iv_add_shopping_cart: ImageView = itemView.findViewById(R.id.iv_add_shopping_cart)
    }
}